package vip.zhenzicheng.rabbitmq.topic;

import com.rabbitmq.client.*;
import vip.zhenzicheng.rabbitmq.utils.RabbitConstant;
import vip.zhenzicheng.rabbitmq.utils.RabbitUtils;

import java.io.IOException;

/**
 * @author zhenzicheng
 * @DESCRIPTION:
 * @DATE: 2022/04/25 4:26 PM
 */
public class Baidu {
  public static void main(String[] args) throws IOException {
    Connection conn = RabbitUtils.getConnection();
    final Channel channel = conn.createChannel();

    channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);
    /*
     * topic模式 #代表匹配一个或者多个 *代表精确匹配一个
     */
    channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_TOPIC, "*.*.*.20201127");
    channel.basicQos(1);
    channel.basicConsume(RabbitConstant.QUEUE_BAIDU, false, new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("百度天气收到气象信息：" + new String(body));
        channel.basicAck(envelope.getDeliveryTag(), false);
      }
    });

  }
}
